package com.example.service.impl;

import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.model.DTO.UserDto;
import com.example.service.IUserService;
import com.example.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ljw
 * @version 1.0
 * @Date 2024/9/12 17:50
 */
@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public List<User> findAll() {
         List<User> all=userMapper.findAll();
         return all;
    }

    @Override
    public int updateById(User user) {
        int i= userMapper.updateById(user);
        return i;
    }

    @Override
    public int deleteById(Integer id) {
        int i=userMapper.deleteById(id);
        return i;
    }

    @Override
    public User findUserByLogin(String username,String password) {
        User user = userMapper.findUserByLogin(username, password);
        return user;
    }

    @Override
    public User findByName(String username) {
        return userMapper.findByName(username);
    }

    @Override
    public int add(String username, String password) {
        // 先对密码进行加密
        String md5String = MD5Utils.code(password);
        int i=userMapper.insertUser(username,md5String);
        return i;
    }

    @Override
    public void saveUser(User user) {
        if (user.getId() == null || user.getId() <= 0) {
            // 插入操作
            userMapper.addUser(user);
        } else {
            // 更新操作
            userMapper.updateUser(user);
        }
    }

}
